package com.mylib.libcore.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.mylib.libcore.R;


/**
 *通用dialog-确定/取消
 */
public class ViewDialogBuilder extends BaseDialogBuilder {

    private String title;
    private View view;
    private int gravity = Gravity.CENTER;
    private boolean mCancelable = true;

    private String okText;
    private String cancelText;
    private ViewDialogBuilder.ActionListener cancelActionListener;
    private ViewDialogBuilder.ActionListener okActionListener;

    public ViewDialogBuilder(Context c) {
        super(c);
    }

    public ViewDialogBuilder setTitle(String title) {
        this.title = title;
        return this;
    }


    public ViewDialogBuilder setCancelable(boolean cancelable) {
        this.mCancelable = cancelable;
        return this;
    }

    public ViewDialogBuilder setView(View view) {
        this.view = view;
        return this;
    }

    public ViewDialogBuilder setContentGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public ViewDialogBuilder setCancel(String text, ViewDialogBuilder.ActionListener listener) {
        this.cancelText = text;
        this.cancelActionListener = listener;
        return this;
    }


    public ViewDialogBuilder setOk(String text, ViewDialogBuilder.ActionListener listener) {
        this.okText = text;
        this.okActionListener = listener;
        return this;
    }

    public Dialog build() {
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_view, null);
        TextView tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        Button btnCancel = (Button) rootView.findViewById(R.id.btn_cancel);
        Button btnOk = (Button) rootView.findViewById(R.id.btn_ok);


        if(cancelActionListener != null) {
            btnCancel.setVisibility(View.VISIBLE);
            btnCancel.setText(cancelText);
        } else {
            btnCancel.setVisibility(View.GONE);
        }

        if(okActionListener != null) {
            btnOk.setVisibility(View.VISIBLE);
            btnOk.setText(okText);
        } else {
            btnOk.setVisibility(View.GONE);
        }

        if(title != null && title.length() > 0) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        } else {
            tvTitle.setVisibility(View.GONE);
        }

        dialog.setContentView(rootView);
        dialog.setCancelable(mCancelable);

        WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
        p.width = (int) (getWindowWidth() * 0.7);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(cancelActionListener == null) {
                    dialog.cancel();
                } else {
                    cancelActionListener.onClick(dialog);
                }
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(okActionListener == null) {
                    dialog.cancel();
                } else {
                    okActionListener.onClick(dialog);
                }
            }
        });
        return dialog;
    }

    public interface ActionListener {
        void onClick(Dialog dialog);
    }
}
